﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Hardanger.Model
{
	public class HardangerSession
	{
        //public ViewModel.ServerFuzzerViewModel ServerFuzzer
        //{
        //    get
        //    {
        //        return ViewModel.ServerFuzzerViewModel.ServerFuzzerViewModelList[FiddlerId];
        //    }
        //}
        public int FiddlerId { get; set; }
        public String HttpCommand { get; set; }
        public String Method { get; set; }
        public String HttpVersion { get; set; }
        public Boolean SSL { get; set; }
        public String Hostname { get; set; }
        public int Port { get; set; }

        private Uri _uri = null;
        public Uri Uri { 
            get 
            {
                if (_uri == null)
                {
                    _uri = new Uri((SSL == true ? "https://" : "http://") + Hostname + Path);
                }
                return _uri;
            }
        }
		public HttpGet Get { get; set; }
        public String Path { get; set; }
		public HttpPost Post { get; set; }
		public String RequestCookies { get; set; }
		public String ResponseCookies { get; set; }
        public Dictionary<String, String> Headers { get; set; }
        
        public HardangerSession()
        {
            Get = new HttpGet();
            Post = new HttpPost();
            Headers = new Dictionary<string, string>();
        }

	}
    public class HttpGet
    {
        public string Raw { get; set; }
        private Dictionary<String, String> _parameters = null;
        public Dictionary<String, String> Parameters
        {
            get
            {
                if (_parameters == null)
                {
                    ParseParameters();
                }
                return _parameters;
            }
        }

        private void ParseParameters()
        {
            _parameters = new Dictionary<string, string>();
            if (String.IsNullOrEmpty(Raw))
            {
                return;
            }
            string[] split = Raw.Split(new char[] { '&' });//TODO: Will an encoded GET mess with this?
            foreach (string pair in split)
            {
                string[] pairsplit = pair.Split(new char[] { '=' }, 2);
                _parameters.Add(pairsplit[0], pairsplit.Length == 2 ? pairsplit[1] : null);
            }
        }
    }
    public class HttpPost
    {
        public string Raw { get; set; }
        private Dictionary<String, String> _parameters = null;
        public Dictionary<String, String> Parameters {
            get
            {
                if (_parameters == null)
                {
                    ParseParameters();
                }
                return _parameters;
            }
        }

        private void ParseParameters()
        {
            _parameters = new Dictionary<string, string>();
            if (String.IsNullOrEmpty(Raw))
            {
                return;
            }
            string[] split = Raw.Split(new char[] { '&' }); //TODO: Will an encoded POST mess with this?
            foreach (string pair in split)
            {
                string[] pairsplit = pair.Split(new char[] { '=' }, 2);
                _parameters.Add(pairsplit[0], pairsplit.Length == 2 ? pairsplit[1] : null);
            }
        }
    }
}
